package io.itit.ecp.admin.provide.action.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import icu.mhb.mybatisplus.plugln.extend.Joins;
import io.itit.grass.common.provide.action.req.BaseBatchIdReq;
import io.itit.grass.common.provide.action.req.BaseIdReq;
import io.itit.grass.common.provide.action.resp.ResultResp;
import io.itit.grass.common.provide.action.validation.*;
import io.itit.grass.common.server.utils.ConvertUtils;
import io.itit.ecp.admin.provide.action.req.EarlyWarningPushQueryReq;
import io.itit.ecp.admin.provide.action.req.EarlyWarningPushReq;
import io.itit.ecp.admin.provide.action.resp.EarlyWarningPushResp;
import io.itit.ecp.admin.provide.filter.IEarlyWarningPushFilter;
import io.itit.ecp.admin.provide.renderer.EarlyWarningPushRenderer;
import io.itit.ecp.admin.server.entity.EarlyWarningPushEntity;
import io.itit.ecp.admin.server.service.IEarlyWarningPushService;
import io.itit.grass.core.server.entity.UserEntity;
import io.itit.grass.estelle.starter.core.ExcelXSSFUtil;
import io.itit.grass.estelle.starter.parse.ExcelParseManage;
import io.itit.grass.estelle.starter.util.JsonUtil;
import io.itit.grass.web.wall.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：预警推送控制类
 * @Date：Create by 2023-12-15 10:51:22
 */
@Api(tags = "预警推送")
@RestController
@RequestMapping("/ecp-admin/earlyWarningPush")
public class EarlyWarningPushController {

	@Resource
	private IEarlyWarningPushService earlyWarningPushService;

	@ApiOperation("分页查询")
	@PostMapping("queryPage")
	@RequiresPermissions("ecp-admin:earlyWarningRecord:search")
	public ResultResp<IPage<EarlyWarningPushResp>> queryPage(@RequestBody @Validated(IQueryPage.class) EarlyWarningPushQueryReq req) {
		Page<EarlyWarningPushResp> page = buildQW(req).joinPage(PageUtils.getPage(req, EarlyWarningPushResp.class), EarlyWarningPushResp.class);
		return ResultResp.success(page);
	}

	@ApiOperation("详情")
	@PostMapping("detail")
	@RequiresPermissions("ecp-admin:earlyWarningPush:search")
	public ResultResp<EarlyWarningPushResp> detail(@RequestBody @Validated(IRequired.class) BaseIdReq req) {
		return ResultResp.success(ConvertUtils.copy(earlyWarningPushService.getById(req.getId()), EarlyWarningPushResp.class));
	}

	/**
     * 组装查询条件
     *
     * @param req
     * @return
     */
	private static JoinLambdaWrapper<EarlyWarningPushEntity> buildQW(EarlyWarningPushQueryReq req) {
		return Joins.of(EarlyWarningPushEntity.class)
				.leftJoin(UserEntity.class, UserEntity::getId, EarlyWarningPushEntity::getUserId)
				.selectAs(UserEntity::getUserName, EarlyWarningPushResp::getUserName)
				.end()
				.eq(StringUtils.isNotEmpty(req.getEarlyWarningId()), EarlyWarningPushEntity::getEarlyWarningId, req.getEarlyWarningId())
				.eq(StringUtils.isNotEmpty(req.getEarlyWarningRecordId()), EarlyWarningPushEntity::getEarlyWarningRecordId, req.getEarlyWarningRecordId())
				.eq(StringUtils.isNotEmpty(req.getUserId()), EarlyWarningPushEntity::getUserId, req.getUserId())
				.eq(StringUtils.isNotEmpty(req.getStatus()), EarlyWarningPushEntity::getStatus, req.getStatus())
				.in(CollectionUtils.isNotEmpty(req.getStatusList()), EarlyWarningPushEntity::getStatus, req.getStatusList())
				.eq(StringUtils.isNotEmpty(req.getDeptId()), EarlyWarningPushEntity::getDeptId, req.getDeptId())
				.in(CollectionUtils.isNotEmpty(req.getIds()), EarlyWarningPushEntity::getId, req.getIds())
				.last(StringUtils.isNotEmpty(req.getSortColumn()), PageUtils.lastOrderBy(req))
				.orderByDesc(StringUtils.isEmpty(req.getSortColumn()), EarlyWarningPushEntity::getCreateTm);
	}

	@ApiOperation("新增")
	@PostMapping("add")
	@RequiresPermissions("ecp-admin:earlyWarningPush:add")
	public ResultResp<Boolean> add(@RequestBody @Validated({IAdd.class}) EarlyWarningPushReq req) {
		return ResultResp.success(earlyWarningPushService.save(ConvertUtils.convertIgnoreId(req, EarlyWarningPushEntity.class)));
	}

	@ApiOperation("编辑")
	@PostMapping("update")
	@RequiresPermissions("ecp-admin:earlyWarningPush:update")
	public ResultResp<Boolean> update(@RequestBody @Validated({IUpdate.class}) EarlyWarningPushReq req) {
		return ResultResp.success(earlyWarningPushService.updateById(ConvertUtils.copy(req, EarlyWarningPushEntity.class)));
	}

	@ApiOperation("删除(根据Id删)")
	@PostMapping("deleteById")
	@RequiresPermissions("ecp-admin:earlyWarningPush:deleteById")
	public ResultResp<Boolean> deleteById(@RequestBody @Validated({IRequired.class}) BaseIdReq req) {
		return ResultResp.success(earlyWarningPushService.removeById(req.getId()));
	}

	@ApiOperation("批量删除")
	@PostMapping("batchDelete")
	@RequiresPermissions("ecp-admin:earlyWarningPush:batchDelete")
	public ResultResp<Boolean> batchDelete(@RequestBody @Validated({IRequired.class}) BaseBatchIdReq req) {
		return ResultResp.success(earlyWarningPushService.removeBatchByIds(req.getIds(), 1000));
	}

	@ApiOperation("导入")
	@PostMapping("upload")
	@RequiresPermissions("ecp-admin:earlyWarningPush:upload")
	public ResultResp<Integer> upload(@RequestParam("file") MultipartFile file, Boolean ignoreError) {
		return ResultResp.success(ExcelParseManage.i().process(EarlyWarningPushRenderer.ENTITY_NAME, file, ignoreError));
	}

	@ApiOperation("导出")
	@PostMapping("export")
	@RequiresPermissions("ecp-admin:earlyWarningPush:export")
	public void export(@RequestBody @Validated(IExport.class) EarlyWarningPushQueryReq req, HttpServletResponse response) {
		ExcelXSSFUtil.export("EarlyWarningPushExport", JsonUtil.toJson(earlyWarningPushService.list(buildQW(req)), new IEarlyWarningPushFilter.EarlyWarningPushExportFilter()), response);
	}
}
